package com.cityguardian.threads;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.cityguardian.model.Usuario;
import com.cityguardian.ws.ConnectionThread;
import com.google.gson.Gson;

import android.os.AsyncTask;
import android.util.Log;

public class AsyncTaskGetUser extends AsyncTask<String, Double, String> {
	
	public final static String EXTRA_MESSAGE = "com.cityguardian.cityguardian.mainMessage";
	private Usuario usuario;
	private String idUsuario;

	public AsyncTaskGetUser(Usuario usuario, String idUsuario){
		this.usuario=usuario;
		this.idUsuario=idUsuario;
	}
	
    @Override
    protected void onPreExecute() {
    }
    
	@Override
	protected String doInBackground(String... params) {
		
		String response=null;
		response=getUser();
		
		return response;
	}
	
	@Override
	protected void onPostExecute(String result) {
		
	}
	
	private String getUser() {
		
		List<NameValuePair> param = new ArrayList<NameValuePair>();								
		param.add(new BasicNameValuePair("idUsuario", idUsuario));
		String route="/ws/usuario/get_user_by_id/";
		
		ConnectionThread connection = new ConnectionThread(param, route);
		String responseBody = connection.getResponse();
		
		if (responseBody==null){
			Log.e("log_tag", "Error in webservice");
			return null;
		}

	    Gson gson = new Gson();
	    Log.e("log_tag", responseBody);
	    usuario = new Usuario();
		usuario = gson.fromJson(responseBody,Usuario.class);
		usuario.toString();
		return "1";
	}

}
